package cn.jx.asc.service.impl;

import cn.jx.asc.common.utils.StringUtils;
import cn.jx.asc.dao.mapper.MountainMapper;
import cn.jx.asc.dao.mapper.PipeMapper;
import cn.jx.asc.dao.mapper.ThresholdMapper;
import cn.jx.asc.pojo.entity.Mountain;
import cn.jx.asc.pojo.entity.Pipe;
import cn.jx.asc.pojo.entity.Threshold;
import cn.jx.asc.service.CustomDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Logger;

/**
 * @author lhg
 */
@Service
public class CustomDataServiceImpl implements CustomDataService {
    private static Logger log = Logger.getLogger("test");
    @Autowired
    private ThresholdMapper thresholdMapper;

    @Autowired
    private PipeMapper pipeMapper;

    @Autowired
    private MountainMapper mountainMapper;

    @Override
    public Double calculateLandslide() {
        final Mountain mountain = getMountain();
        final Pipe pipe = gePipe();
        final Double aDouble = calculateLandslide(pipe, mountain);
        return aDouble;
    }


    @Override
    public Double calculateLandslide(Pipe pipe, Mountain mountain) {
        double PROBABILITY_OF_LANDSLIDE = 0;
        Threshold threshold = thresholdMapper.selectById(1);
        final String maxAirTemperature = threshold.getMaxAirTemperature();
        final String maxAirHumidity = threshold.getMaxAirHumidity();
        final String minAirTemperature = threshold.getMinAirTemperature();
        final String maxInjectionVolume = threshold.getMaxInjectionVolume();
        final String maxSoilHumidity = threshold.getMaxSoilHumidity();
        final String minSoilTemperature = threshold.getMinSoilTemperature();
        final String maxSoilTemperature = threshold.getMaxSoilTemperature();
        final String maxAngle = threshold.getMaxAngle();
        final String maxRainfull = threshold.getMaxRainfull();
        if (StringUtils.isEmpty(maxAirTemperature)||StringUtils.isEmpty(maxAirHumidity)||StringUtils.isEmpty(minAirTemperature)||maxInjectionVolume== null||
                StringUtils.isEmpty(maxSoilHumidity)||StringUtils.isEmpty(minSoilTemperature)||StringUtils.isEmpty(maxSoilTemperature)||StringUtils.isEmpty(maxAngle)||maxRainfull==null) {
            return -1.0;
        }

        if (pipe.getAngle()*0.1 > 1) {
            final Integer angle = pipe.getAngle();
            final double v = (angle*0.1 - 1) / (new Double(maxAngle) - 1) * 0.7;
            PROBABILITY_OF_LANDSLIDE +=v;
        }
        if (PROBABILITY_OF_LANDSLIDE<1){
            final Integer mountainRainfull = mountain.getRainfull();
            if (mountainRainfull!=null){
                final double rainfull = mountainRainfull*0.1;
                if (rainfull<=10&&rainfull>0){
                    PROBABILITY_OF_LANDSLIDE+=rainfull*0.1*0.3*0.05;
                }
                if (rainfull>10&&rainfull<=40){
                    PROBABILITY_OF_LANDSLIDE+=((rainfull-10)/30)*0.6*0.05;
                }
                if (rainfull>40&&new Double(maxRainfull)*0.1>40){
                    PROBABILITY_OF_LANDSLIDE+=(((rainfull-40)/(new Double(maxRainfull)*0.1-40)))*0.05;
                }
            }

        }

        if (PROBABILITY_OF_LANDSLIDE<1){
            final Integer totalInjectionVolume = mountain.getTotalInjectionVolume();
            final double maxVolume = new Double(maxInjectionVolume) * 0.1;
            if (totalInjectionVolume!=null){
                final double totalVolume = totalInjectionVolume * 0.1;
                if (totalVolume<maxVolume){
                    if (Math.abs((maxVolume-totalVolume))<1){
                        PROBABILITY_OF_LANDSLIDE+= (maxVolume - totalVolume)*0.7*0.15;
                    }
                }
                if (totalVolume<maxVolume){
                    if (Math.abs((maxVolume-totalVolume))<1){
                        PROBABILITY_OF_LANDSLIDE+= (maxVolume - totalVolume)*0.15;
                    }
                }
            }
        }

        if (PROBABILITY_OF_LANDSLIDE<1){
            final Integer airTemperature = mountain.getAirTemperature();
            final double maxTemperature = new Double(maxAirTemperature) * 0.1;
            final double minTemperature = new Double(minAirTemperature) * 0.1;
            if (airTemperature!=null){
                final double aTemperature = airTemperature * 0.1;
                if (airTemperature<maxTemperature&&airTemperature>minTemperature){
                   double d1 = (Math.abs(maxTemperature)+Math.abs(minTemperature))*0.5;
                    double d2 =Math.abs (Math.abs(maxTemperature)-Math.abs(minTemperature))*0.02;
                    PROBABILITY_OF_LANDSLIDE+= Math.abs(aTemperature-d1)/d2*0.03;
                }
            }
        }
        if (PROBABILITY_OF_LANDSLIDE<1){
            final Integer soilTemperature = mountain.getSoilTemperature();
             double xTemperature = new Double(maxSoilTemperature) * 0.1;
             double nTemperature = new Double(minSoilTemperature) * 0.1;
            if (soilTemperature!=null){
                final double aTemperature = soilTemperature * 0.1;
                if (soilTemperature<xTemperature&&soilTemperature>nTemperature){
                    double d1 = (Math.abs(xTemperature)+Math.abs(nTemperature))*0.5;
                    double d2 =Math.abs (Math.abs(xTemperature)-Math.abs(nTemperature))*0.5;
                    PROBABILITY_OF_LANDSLIDE+= Math.abs(aTemperature-d1)/d2*0.03;
                }
            }
        }

        if (PROBABILITY_OF_LANDSLIDE<1){
            final Integer soilHumidity = mountain.getSoilHumidity();
            final double maxHumidty =new Double(maxAirHumidity) * 0.1;
            if (soilHumidity!=null){
                double humidity =soilHumidity*0.1;
                if (humidity>40&&maxHumidty>humidity){
                    PROBABILITY_OF_LANDSLIDE+=((humidity-40)/(maxHumidty-40))*0.03;
                }
            }
        }
        if (PROBABILITY_OF_LANDSLIDE<1){
            final Integer aHumidity = mountain.getAirHumidity();
            final double maxHumidty =new Double(maxSoilHumidity) * 0.1;
            if (aHumidity!=null){
                double humidity =aHumidity*0.1;
                if (humidity>40&&maxHumidty>humidity){
                    PROBABILITY_OF_LANDSLIDE+=((humidity-40)/(maxHumidty-40))*0.01;
                }
            }
        }
        if(PROBABILITY_OF_LANDSLIDE>1){
            return 1.00;
        }
        return PROBABILITY_OF_LANDSLIDE;
    }


    @Override
    public String abnormalLiquidLevel() {
        Threshold threshold = thresholdMapper.selectById(1);

        final String minLiquidLevel = threshold.getMinLiquidLevel();
        final String maxLiquidLevel = threshold.getMaxLiquidLevel();
        if ( minLiquidLevel== null||maxLiquidLevel== null) {
            return "阈值存在错误";
        }
        Pipe pipe =null;
        final QueryWrapper<Pipe> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created_time");
        final List<Pipe> pipes = pipeMapper.selectList(wrapper);
        if (pipes!=null){
            pipe = pipes.get(0);
            final double v = pipe.getLiquidLevel() * 0.1;
            if (v<new Double(minLiquidLevel)*0.1||v>new Double(maxLiquidLevel)*0.1){
                return "液面高度异常";
            }
        }

        return null;
    }

    @Override
    public String abnormalAirTemperature() {
        Threshold threshold = thresholdMapper.selectById(1);
        final String maxAirTemperature = threshold.getMaxAirTemperature();
        final String minAirTemperature = threshold.getMinAirTemperature();
         Mountain mountain = getMountain();
        if (mountain!=null){
            final Integer airTemperature = mountain.getAirTemperature();
            if (airTemperature!=null){
                if (airTemperature*0.1>new Double(maxAirTemperature)*0.1||airTemperature*0.1<new Double(minAirTemperature)*0.1){
                    return "气温异常";
                }
            }
        }
        return null;
    }

    @Override
    public String airHumidityIsAbnormal() {
        Threshold threshold = thresholdMapper.selectById(1);
        final String maxAirHumidity = threshold.getMaxAirHumidity();
        final Mountain mountain = getMountain();
        if (mountain!=null){
            final Integer airHumidity = mountain.getAirHumidity();
            if (airHumidity!=null){
                if (airHumidity>new Double(maxAirHumidity)){
                    return "空气湿度异常";
                }
            }
        }
        return null;
    }

    @Override
    public String abnormalSoilTemperature() {
        Threshold threshold = thresholdMapper.selectById(1);
        final String maxAirTemperature = threshold.getMaxAirTemperature();
        final String minAirTemperature = threshold.getMinAirTemperature();
        final Mountain mountain = getMountain();
        if (mountain!=null){
            final Integer airTemperature = mountain.getAirTemperature();
            if (airTemperature!=null){
                if (airTemperature>new Double(maxAirTemperature)||airTemperature<new Double(minAirTemperature)){
                    return "土壤温度异常";
                }
            }
        }
        return null;
    }

    @Override
    public String abnormalSoilMoisture() {
        Threshold threshold = thresholdMapper.selectById(1);
        final String maxSoilHumidity = threshold.getMaxSoilHumidity();
        final Mountain mountain = getMountain();
        if (mountain!=null){
            final Integer soilHumidity = mountain.getSoilHumidity();
            if (soilHumidity!=null){
                if (soilHumidity>new Double(maxSoilHumidity)){
                    return "土壤湿度异常";
                }
            }

        }
        return null;
    }

    @Override
    public String abnormalInjectionVolume() {
        Threshold threshold = thresholdMapper.selectById(1);
        final String maxInjectionVolume = threshold.getMaxInjectionVolume();
        final Mountain mountain = getMountain();
        if (mountain!=null){
            final Integer totalInjectionVolume = mountain.getTotalInjectionVolume();
            if (totalInjectionVolume!=null){
                if (totalInjectionVolume>new Double(maxInjectionVolume)){
                    return "注液量异常";
                }
            }
        }
        return null;
    }

    public  Mountain getMountain(){
        final QueryWrapper<Mountain> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created_time");
        final List<Mountain> mountains = mountainMapper.selectList(wrapper);
        if (mountains!=null){
            final Mountain mountain = mountains.get(0);
            if (mountain!=null){
                return mountain;
            }
        }
        return null;
    }
    public  Pipe gePipe(){
        final QueryWrapper<Pipe> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created_time");
        final List<Pipe> pipes = pipeMapper.selectList(wrapper);
        if (pipes!=null){
            final Pipe pipe = pipes.get(0);
            if (pipe!=null){
                return pipe;
            }
        }
        return null;
    }
}

